#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include "colorspace.h"

int main(int argc, char *argv[])
{
    if(strcmp(argv[1],"xyY") == 0) {
        struct xyY xyY;
        int r = strtoul(argv[2],NULL,0);
        int g = strtoul(argv[3],NULL,0);
        int b = strtoul(argv[4],NULL,0);
        float frgb[3] = {r / 255.0,g / 255.0,b / 255.0};
        rgb2xyY(frgb,&xyY);
        printf("x: %f y: %f Y: %f\n",xyY.x,xyY.y,xyY.Y);

    } else if(strcmp(argv[1],"rgb") == 0) {
        struct xyY xyY;
        xyY.x = strtof(argv[2],NULL);
        xyY.y = strtof(argv[3],NULL);
        xyY.Y = strtof(argv[4],NULL);
        float frgb[3];
        xyY2rgb(&xyY,frgb);
        printf("R: %d G: %d B: %d\n",(uint32_t)(frgb[0] * 255.0),
               (uint32_t)(frgb[1] * 255.0),(uint32_t)(frgb[2] * 255.0));
    } else if(strcmp(argv[1],"led") == 0) {

        /* struct LED_Param led = { */
        /*     {0.6900,0.3100,50.0}, */
        /*     {0.1900,0.6100,150.0}, */
        /*     {0.1530,0.0278,40}, */
        /* }; */

        struct LED_Param led = {
            {0.6400,0.3100,50.0},
            {0.1900,0.6100,150.0},
            {0.1530,0.0278,40.0},
        };

        struct xyY xyY;
        int r = strtoul(argv[2],NULL,0);
        int g = strtoul(argv[3],NULL,0);
        int b = strtoul(argv[4],NULL,0);
        float frgb[3] = {r / 255.0,g / 255.0,b / 255.0};
        int16_t data[3];
        printf("rgb: %d %d %d\n",r,g,b);
        rgb2xyY(frgb,&xyY);
        xyY2data(&led,&xyY,data,8);
        printf("Dr: %d Dg: %d Db %d\n",data[0],data[1],data[2]);
    } else {
        printf("%s xyY [R] [G] [B]\n",argv[0]);
        printf("%s rgb [x] [y] [Y]\n",argv[0]);
        printf("%s led [R] [G] [B]\n",argv[0]);
    }
    return 0;
}
//gcc -o c main.c colorspace.c -lm
